from abc import ABCMeta, abstractmethod

from utils.http_visit import ApiRequest, ApiResponse
from utils.log import LogFactory


class AbstractApiInterceptor:
    ...


class AbstractApiBeforeInterceptor(AbstractApiInterceptor, metaclass=ABCMeta):
    @abstractmethod
    def before_intercept(self, req: ApiRequest) -> ApiRequest: ...


class AbstractApiAfterInterceptor(AbstractApiInterceptor, metaclass=ABCMeta):
    @abstractmethod
    def after_intercept(self, req: ApiRequest, res: ApiResponse) -> (ApiRequest, ApiResponse): ...


class DefaultApiInterceptor(AbstractApiAfterInterceptor):
    log = LogFactory.get_log()

    def after_intercept(self, req: ApiRequest, res: ApiResponse) -> (ApiRequest, ApiResponse):
        self.log.info(f"请求地址:{req.url}")
        self.log.info(f"状态:{res.response_code}")
        return req, res


class Mock(AbstractApiBeforeInterceptor, AbstractApiAfterInterceptor):
    log = LogFactory.get_log()

    def __init__(self, response_code=None, response_body=None):
        self.code = response_code
        self.body = response_body

    def before_intercept(self, req: ApiRequest) -> ApiRequest:
        def proxy():
            pass
        req.do_send = proxy
        return req

    def after_intercept(self, req: ApiRequest, res: ApiResponse) -> (ApiRequest, ApiResponse):
        self.log.info(f"The api has been mocked. {req.url}")
        res = ApiResponse(req.url, self.code, self.body)
        return req, res
